/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import DataAccess.DBUtil;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author Dat-masuto
 */
public class Classification {
     public static boolean insert(Entity.Classification c){
        String sql = "{call pcInsertClassification(?)}";
        try{
            CallableStatement cs =DBUtil.getConnection(DBUtil.getInfoDB()).prepareCall(sql);

            cs.setString(1,c.getClassificationName());
            return (cs.executeUpdate()==1);
        }catch(Exception ex){
            //JOptionPane.showMessageDialog(null, ex.getMessage());
            return false;
        }
    }


     public static boolean update(Entity.Classification c){
        String sql = "{call pcUpdateClassification(?,?)}";
        try{
            CallableStatement cs = DBUtil.getConnection(DBUtil.getInfoDB()).prepareCall(sql);

            cs.setInt(1,c.getClassificationID());
            cs.setString(2,c.getClassificationName());

            return (cs.executeUpdate()==1);
        }catch(Exception ex){
            //JOptionPane.showMessageDialog(null, ex.getMessage());
            return false;
        }
    }

        public static boolean delete(Entity.Classification c){
        String sql = "{call pcDeleteClassification(?)}";
        try{
            CallableStatement cs = DBUtil.getConnection(DBUtil.getInfoDB()).prepareCall(sql);

            cs.setInt(1,c.getClassificationID());

            return (cs.executeUpdate()==1);
        }catch(Exception ex){
            //JOptionPane.showMessageDialog(null, ex.getMessage());
            return false;
        }
    }

        public static ArrayList<Entity.View_Classification> getAll(){
        String sql = "{call pcGetAllView_Classification}";
        ArrayList<Entity.View_Classification> lst = new ArrayList<Entity.View_Classification>();

        try{
            CallableStatement cs = DBUtil.getConnection(DBUtil.getInfoDB()).prepareCall(sql);

            ResultSet rsClassification = cs.executeQuery();
            while(rsClassification.next()){
                Entity.View_Classification v = new Entity.View_Classification();
                v.setCode(rsClassification.getInt("ClassificationID"));
                v.setName(rsClassification.getString("Name"));
                v.setQuantityvacacies(rsClassification.getInt("countVacacies"));
                v.setQuantityjobseeker(rsClassification.getInt("countJobseeker"));


                lst.add(v);

            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
            return lst;
    }

        public static ArrayList<Entity.Classification> getAllClassName(){
        String sql = "{call pcGetAllClassificationName}";
        ArrayList<Entity.Classification> lst = new ArrayList<Entity.Classification>();

        try{
            CallableStatement cs = DBUtil.getConnection(DBUtil.getInfoDB()).prepareCall(sql);

            ResultSet rsClassification = cs.executeQuery();
            while(rsClassification.next()){
                Entity.Classification c = new Entity.Classification();
                //c.setClassificationID(rsClassification.getInt("ClassificationID"));
                c.setClassificationName(rsClassification.getString("Name"));


                lst.add(c);

            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
            return lst;
    }

        public static int  getClassID(String name){
            String sql = "select ClassificationID from Classification where name = " + "'" + name + "'" ;

       int ClassificationID = 0 ;

        try{
            CallableStatement cs = DBUtil.getConnection(DBUtil.getInfoDB()).prepareCall(sql);



            ResultSet rsClassificationID = cs.executeQuery();

                while(rsClassificationID.next())
                ClassificationID = rsClassificationID.getInt("ClassificationID");


        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
            return ClassificationID;
    }

         public static String getClassName(int id){
            String sql2 = "select Name from Classification where ClassificationID = "  + id  ;

       String ClassificationName = null  ;

        try{
            CallableStatement cs = DBUtil.getConnection(DBUtil.getInfoDB()).prepareCall(sql2);



            ResultSet rsClassificationName = cs.executeQuery();

                while(rsClassificationName.next())
                ClassificationName = rsClassificationName.getString("Name");


        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
            return ClassificationName;
    }

}
